import { qUpsert } from './QDRANT.js';
import { QDRANT_COLLECTION_NAME } from './conf.js';
import vbE from '@3-/vb/vbE.js'
import { jinaIndex } from './jina.js'
import splitTxt from '@3-/split_txt'


// 索引服务实现
export default async (call, callback) => {
  const doc = call.request,
    sentence_li = splitTxt(doc.txt);

  if (sentence_li.length > 0) {

    const embed_li = await jinaIndex(sentence_li);

    // 准备数据点
    const points = sentence_li.map((sentence, i) => {
      let id = Buffer.from(vbE([doc.id, i]));
      id = id.readUIntBE(0, id.byteLength);
      console.log(id, sentence)
      return {
        id, // 文档ID-句子id
        vector: embed_li[i],
        payload: {
          doc_id: doc.id,
          user_id: doc.user_id,
          repo_id: doc.repo_id,
        },
      }
    });

    // Upsert 数据到 Qdrant
    await qUpsert(QDRANT_COLLECTION_NAME, {
      wait: true,
      points: points,
    });

  }
  callback(null, {});
}

